/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.server.view.tags;

import java.util.StringTokenizer;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.PropertyType;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

/**
 *
 * @author Walter Luetgenau
 */
public class SearchResultTag extends TagSupport {
    private String pattern;
    private Node resultNode;
    
    /** Creates a new instance of BodyTag */
    public SearchResultTag() {
    }
    
    protected String normalizePattern(String pattern) {
        if ( pattern.charAt(0) == '\"' && pattern.charAt(pattern.length()-1) == '\"')
            return pattern.substring(1, pattern.length()-1);
        return pattern;
    }
    
    protected String highlightPattern( String value, String pattern) {
        int index;
        
        pattern = normalizePattern(pattern);
        
        StringBuffer result = new StringBuffer();
        StringTokenizer tokenizer = new StringTokenizer(value, "\n.", true);
        while( tokenizer.hasMoreTokens()) {
            String token = tokenizer.nextToken();
            
            boolean found = false;
            while( (index = token.indexOf(pattern)) >= 0) {
                found = true;
                
                result.append(token.substring(0,index));
                result.append("<b>");
                result.append(pattern);
                result.append("</b>");

                token = token.substring(index+pattern.length());
            }
            
            if ( found ) {
                result.append(token);
                result.append(tokenizer.nextToken() );
            }
       }
        
        return result.toString();
    }
    
    public int doStartTag() throws JspException {
        try {
            StringBuffer result = new StringBuffer();

            PropertyIterator i = resultNode.getProperties();
            while( i.hasNext()) {
                Property property = (Property) i.next();
                if ( property.getType() != PropertyType.STRING)
                    continue;
                result.append(highlightPattern( (String) property.getValue().getString(), pattern));
            }

            pageContext.getOut().print(result.toString());

            return SKIP_BODY;
        }
        catch( Exception e) {
            throw new JspException(e);
        }
    }

    public String getPattern() {
        return pattern;
    }

    public void setPattern(String pattern) {
        this.pattern = pattern;
    }

    public Node getResultNode() {
        return resultNode;
    }

    public void setResultNode(Node resultNode) {
        this.resultNode = resultNode;
    }
    
}
